package o2sjsc.projects.erp;

import java.util.HashMap;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.widget.RelativeLayout;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;

@SuppressLint("NewApi")
public class MainActivity extends Activity implements TabListener {
	RelativeLayout rl;
	HashMap<String, String> hash;
	// Context mContext ;
	String tabName = "";
	SearchView searchView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		try {
			rl = (RelativeLayout) findViewById(R.id.mainLayout);
			fragMentTra = getFragmentManager().beginTransaction();
			ActionBar bar = getActionBar();
			bar.addTab(bar.newTab().setText("HD").setTabListener(this));
			bar.addTab(bar.newTab().setText("KH").setTabListener(this));
			bar.addTab(bar.newTab().setText("CV").setTabListener(this));

			bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM
					| ActionBar.DISPLAY_USE_LOGO);
			bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
			bar.setDisplayShowHomeEnabled(true);
			bar.setDisplayShowTitleEnabled(false);
			bar.show();

		} catch (Exception e) {
			e.getMessage();
		}
	}

	FragHD fram1;
	FragmentTransaction fragMentTra = null;
	FragmentManager fm;
	FragKH fram2;
	FragCV fram3;

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		searchView = (SearchView) menu.findItem(R.id.search).getActionView();
		searchView.setIconified(true);
		 Fragment target = getFragmentManager().findFragmentByTag(tabName);
		 searchView.setOnQueryTextListener((OnQueryTextListener) target);
		return super.onCreateOptionsMenu(menu);
		// return true;
	}

	@Override
	public void onTabReselected(Tab tab, FragmentTransaction ft) {
	}

	@Override
	public void onTabSelected(Tab tab, FragmentTransaction ft) {

		if (tab.getText().equals("HD")) {
			try {
				rl.removeAllViews();
			} catch (Exception e) {
			}
			// FragHD yourFragment = (FragHD) getFragmentManager()
			// .findFragmentByTag("HD");

			// if (yourFragment == null) {
			/*
			 * Fragment target = fram1; FragmentTransaction transaction =
			 * getFragmentManager().beginTransaction(); if(target.isAdded()) {
			 * transaction.show(target); } else {
			 * transaction.addToBackStack("HD");
			 * transaction.replace(R.id.page_fragment, target); }
			 */

			fram1 = new FragHD();
			fragMentTra.addToBackStack(null);
			fragMentTra = getFragmentManager().beginTransaction();
			fragMentTra.add(rl.getId(), fram1,"HDTag");
			fragMentTra.commit();
			//fram1.setRetainInstance(true);
			//fram1.
			// mContext = fram1.getActivity();
			// }
		} else if (tab.getText().equals("KH")) {
			try {
				rl.removeAllViews();
			} catch (Exception e) {
			}
			fram2 = new FragKH();
			fragMentTra.addToBackStack("KH");
			fragMentTra = getFragmentManager().beginTransaction();
			fragMentTra.add(rl.getId(), fram2,"KHTag");
			fragMentTra.commit();
			// mContext = fram1.getActivity();
		} else if (tab.getText().equals("CV")) {
			try {
				rl.removeAllViews();
			} catch (Exception e) {
			}
			fram3 = new FragCV();
			fragMentTra.addToBackStack("CV");
			fragMentTra = getFragmentManager().beginTransaction();
			fragMentTra.add(rl.getId(), fram3,"CVTag");
			fragMentTra.commit();
			// mContext = fram1.getActivity();
		}

	}

	@Override
	public void onTabUnselected(Tab tab, FragmentTransaction ft) {

	}

	/*
	 * @Override protected void onSaveInstanceState(Bundle outState) {
	 * super.onSaveInstanceState(outState);
	 * 
	 * //Save the fragment's instance getFragmentManager().putFragment(outState,
	 * "mContext", mContext );
	 * 
	 * 
	 * }
	 */
	@Override
	public void onAttachFragment(Fragment fragment) {
		// TODO Auto-generated method stub
		super.onAttachFragment(fragment);
		tabName = fragment.getTag();
		
	}
}
